import {Popover} from "antd";
import {messages} from "share/common";
import moment from "moment";
import React, {PropTypes} from "react";
import 'styles/components/template/slide-document/resignation-mark.scss'
import {connect} from "react-redux";
import Condition from "components/condition";
const If = Condition.If;

function isValidEntry (entry) {
  return ['invoice-bag-audit', 'approve', 'audit', 'view'].indexOf(entry) > -1
}

function resignationMark(props) {
  const { applicant = {}, options = { 'approvalControlOpt': {} }, entry } = props;
  let showUserStatus = entry !== 'approve' || options['approvalControlOpt']['showUserStatus'];
  return (
    <If value={~[1002, 1003].indexOf(applicant.status) && isValidEntry(entry) && showUserStatus}>
      <Popover
        content={messages('common-1.key571',{arg1: applicant.leavingDate ? moment(applicant.leavingDate).format('YYYY-MM-DD') : '-'})/*离职日期：{arg1}*/}
      >
        <div className="resignation-mark" >
          {applicant.status === 1002 ? messages('common-1.key572')/*即将离职*/ : messages('common-1.key967')/*已离职*/}
        </div>
      </Popover>
    </If>
  )
}

class ResignationMark extends React.Component {
  static contextTypes = {
    entry: PropTypes.string
  };

  render() {
    let { entry } = this.context;
    if (entry === undefined) {
      entry = (location.pathname.indexOf('approve') > -1) && 'approve';
    }
    return resignationMark({ entry, ...this.props, options: this.props.loginOptions })
  }
}

function mapStateToProps(state) {
  return {
    loginOptions: state.login.loginOptions,
  }
}
export default connect(mapStateToProps)(ResignationMark)
